#!/bin/sh

: ${SMLSHARP:=./minismlsharp}
: ${SMLFLAGS:=}
: ${files_mk:=files.mk}
: ${depend_mk:=depend.mk}

LF='
'

smi=
i=2
while [ "$i" -le "$#" ]; do
  smi="$smi \"\${$i}\""
  i=$(($i+2))
done

set -e
libfiles=`mktemp`
allfiles=`mktemp`
makefile=`mktemp`
trap 'rm -f "$libfiles" "$allfiles" "$makefile"' EXIT
trap 'exit 127' INT QUIT STOP

smlsharp='$SMLSHARP -Bsrc -nostdpath $SMLFLAGS -Mm -dprintWidth=0'
eval "echo $smlsharp $smi 1>&2"
eval "$smlsharp $smi > \"\$makefile\""

awk -v depend_mk="$depend_mk" \
    -v files_mk="$files_mk" \
    -v libfiles="$libfiles" \
    -v allfiles="$allfiles" \
    -v makefile="$makefile" \
'
  BEGIN {
    for (i = 1; i < ARGC; i += 2) {
      sub("\\.smi$", ":", ARGV[i+1]);
      projects[ARGV[i+1]] = ARGV[i];
      delete ARGV[i];
      delete ARGV[i+1];
    }
    ARGV[1] = makefile;
    print "# auto-generated by mkdepend. DO NOT EDIT BY HAND." > depend_mk;
    print "# auto-generated by mkdepend. DO NOT EDIT BY HAND." > files_mk;
  }
  /^\t/ { next }
  /^[^ :]*\.o:/ {
    gsub(" ", " \\\n ");
    print $0" \\\n $(SMLSHARP_DEP)" >> depend_mk;
    next;
  }
  $1 in projects {
    proj = projects[$1];
    isLib = match(proj, "_LIB$");
    items = split($0, a);
    for (i = 2; i <= items; i++)
      files[a[i]] += isLib;
    out = "";
    for (i = 2; i <= items; i++) {
      if (match(a[i], "\\.o$"))
        out = out" \\\n"a[i];
    }
    print proj"_OBJECTS ="out >> files_mk; 
  }
  END {
    for (i in files) {
      if (files[i] != 0) print i >> libfiles;
      print i >> allfiles;
    }
  }
' "$@"

GEN_SOURCES='
src/config/main/Version.sml
src/config/main/SQLConfig.sml
src/compiler/compilerIRs/absyn/main/AbsynConstFormatter.sml
src/compiler/compilerIRs/absyn/main/AbsynFormatter.sml
src/compiler/compilerIRs/absyn/main/AbsynSQLFormatter.sml
src/compiler/compilerIRs/absyn/main/AbsynTyFormatter.sml'
GEN_MISC_FILES=
ALL_OBJECTS=`sed '/:/!d;s/:.*$//' "$depend_mk"`
IFS=$LF

for i in `echo "$ALL_OBJECTS" | sed 's/\.o$//'`; do
  case "$i" in
    src/ml-yacc/src/yacc.lex) ;;
    src/ml-yacc/src/yacc.grm) ;;
    src/smlnj-lib/JSON/json.lex) ;;
    *.ppg)
      GEN_SOURCES="$GEN_SOURCES$LF$i.sml"
      echo "$i.sml: \\" >> "$depend_mk"
      echo " $i \\" >> "$depend_mk"
      echo ' $(SMLFORMAT_DEP)' >> "$depend_mk"
      echo "	\$(SMLSHARP_ENV) \$(MINISMLFORMAT) --output=$i.sml \$(srcdir)/$i" >> "$depend_mk"
      ;;
    *.lex)
      GEN_SOURCES="$GEN_SOURCES$LF$i.sml"
      echo "$i.sml: \\" >> "$depend_mk"
      echo " $i \\" >> "$depend_mk"
      echo ' $(SMLLEX_DEP)' >> "$depend_mk"
      echo "	\$(SMLSHARP_ENV) \$(MINISMLLEX) -o $i.sml \$(srcdir)/$i" >> "$depend_mk"
      ;;
    *.grm)
      GEN_SOURCES="$GEN_SOURCES$LF$i.sml"
      GEN_MISC_FILES="$GEN_MISC_FILES$LF$i.desc"
      echo "$i.sml: \\" >> "$depend_mk"
      echo " $i \\" >> "$depend_mk"
      echo ' $(SMLYACC_DEP)' >> "$depend_mk"
      echo "	\$(SMLSHARP_ENV) \$(MINISMLYACC) -s -p $i \$(srcdir)/$i" >> "$depend_mk"
      ;;
  esac
done

(echo 'INSTALL_FILES_OBJ ='; sed '/\.o$/!d' "$libfiles" | sort) \
| sed '$!s/$/ \\/' >> "$files_mk"
(echo 'INSTALL_FILES_SRC ='; sed '/\.o$/d' "$libfiles" | sort) \
| sed '$!s/$/ \\/' >> "$files_mk"
echo "ALL_OBJECTS =$LF$ALL_OBJECTS" | sed '$!s/$/ \\/' >> "$files_mk"
echo "GEN_SOURCES =$GEN_SOURCES" | sed '$!s/$/ \\/' >> "$files_mk"
echo "GEN_MISC_FILES =$GEN_MISC_FILES" | sed '$!s/$/ \\/' >> "$files_mk"

if [ -d ../.hg ]; then
  export HG_PLAIN=1
  set +e
  cat "$allfiles" | sort | sed '
    /\.o$/s/\.o$/.sml/;
    \,^src/ml-yacc/src/yacc\.lex\.sml$,d;
    \,^src/ml-yacc/src/yacc\.grm\.sml$,d;
    \,^src/config/main/Version\.sml$,s/$/.in/;
    \,^src/config/main/SQLConfig\.sml$,s/$/.in/;
    \,^src/compiler/compilerIRs/absyn/main/AbsynFormatter\.sml$,d;
    \,^src/compiler/compilerIRs/absyn/main/AbsynConstFormatter\.sml$,d;
    \,^src/compiler/compilerIRs/absyn/main/AbsynTyFormatter\.sml$,d;
    \,^src/compiler/compilerIRs/absyn/main/AbsynSQLFormatter\.sml$,d;
    /\.ppg\.sml$/s/\.sml$//;
    /\.grm\.sml$/s/\.sml$//;
    /\.grm.sig/d;
    /\.lex\.sml$/s/\.sml$//;
  ' | xargs hg status -rdui 2>&1 | grep '^[^#]'
  if [ "$?" = "0" ]; then
    echo "*** Add these files to the repository."
    exit 1
  fi
fi
